package com.qwt.server.advice;

import com.qwt.server.response.R;
import com.qwt.server.utils.ExceptionsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 异常处理类
 * @author 时光
 * @date 2021/12/10 15:3445
 */
@RestControllerAdvice
@Slf4j
public class FExceptionHandler {
    @ExceptionHandler(FException.class)
    public R handleRRException(FException e){
        log.error("捕获到异常信息:{},抛出异常:{}", e.getMessage(), ExceptionsUtil.getStackTraceAsString(e));
        return R.error(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public R handlerNoFoundException(Exception e) {
        log.error(e.getMessage(), e);
        return R.error(404, "路径不存在，请检查路径是否正确");
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public R handleDuplicateKeyException(DuplicateKeyException e){
        log.error(e.getMessage(), e);
        return R.error("数据库中已存在该记录");
    }

    @ExceptionHandler(Exception.class)
    public R handleException(Exception e){
        log.error(e.getMessage(), e);
        return R.error(e.getMessage());
    }
}
